package edu.whu.puppygo.controller;

import edu.whu.puppygo.base.HttpResponse;
import edu.whu.puppygo.dao.PetDao;
import edu.whu.puppygo.dao.UserDao;
import edu.whu.puppygo.dto.UpdatePetDto;
import edu.whu.puppygo.entity.Pet;
import edu.whu.puppygo.entity.User;
import edu.whu.puppygo.service.PetService;
import edu.whu.puppygo.util.GlobalUser;
import edu.whu.puppygo.util.OssUtil;
import edu.whu.puppygo.vo.PetVo;
import edu.whu.puppygo.vo.UpdateAvatorVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

import static edu.whu.puppygo.util.ObjectFieldUpdater.getNullPropertyNames;

/**
 * @Author xinjiezhuang
 * @Date 2023-11-20
 */
@RestController
@Slf4j
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetDao petDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private PetService petService;

    @Resource
    private OssUtil ossUtil;


    @GetMapping("/{petId}")
    public HttpResponse<Optional<Pet>> getPetById(@PathVariable long petId) {
        Optional<Pet> pet = petService.getPetById(petId);
        return HttpResponse.success(pet);
    }

    @ApiModelProperty("创建宠物")
    @PostMapping("")
    public HttpResponse<PetVo> createPet(Pet pet, @RequestParam(value = "file", required = false) MultipartFile mpfile) {
        PetVo petVo = new PetVo();
        List<Pet> pets = petService.create(pet, mpfile);
        petVo.setPets(pets);
        return HttpResponse.success(petVo);
    }


    @ApiModelProperty("更新宠物信息")
    @PutMapping("/update")
    public HttpResponse<Pet> updatePet(@RequestBody UpdatePetDto updatePetDto) throws IllegalAccessException {
        Optional<Pet> existingPetOptional = petDao.findById(updatePetDto.getId());
        Pet existingPet = existingPetOptional.get();
        BeanUtils.copyProperties(updatePetDto, existingPet, getNullPropertyNames(updatePetDto));
        Pet updatePet = petService.updatePet(existingPet);
        return HttpResponse.success(updatePet);
    }


    @ApiModelProperty("更新宠物头像")
    @PostMapping("/update/avatar")
    public HttpResponse<UpdateAvatorVo> updateAvatar(Long petId, @RequestParam(value = "file", required = false) MultipartFile mpfile) {
        User globalUser = GlobalUser.getUser();
        UpdateAvatorVo updateAvatorVo = petService.updatePetAvatarWithFile(petId, mpfile);
        return HttpResponse.success(updateAvatorVo);
    }

    @ApiModelProperty("删除宠物")
    @DeleteMapping("/{petId}")
    public HttpResponse<PetVo> deletePet(@PathVariable String petId) {
        PetVo petVo = petService.deletePetById(petId);
        return HttpResponse.success(petVo);
    }

    @PatchMapping("/updateWeight")
    public HttpResponse<Pet> updatePetWeight(long petId, String newWeight) {
        Pet updated = petService.updatePetWeight(petId, newWeight);
        return HttpResponse.success(updated);
    }

    @ApiModelProperty("获取个人宠物列表")
    @GetMapping("/pets")
    public HttpResponse<PetVo> getPets() {
        User globalUser = GlobalUser.getUser();
        PetVo petVo = new PetVo();
        petVo.setPets(petService.getAllPetsByUserId(globalUser.getId()));
        return HttpResponse.success(petVo);
    }


}
